#pragma once

#include <string>
#include <time.h>

#include "yasli/Files.h"
#include "TagStorage.h"
#include "DirectoryWatch.h"

class TagSourceNode;
class TagSourceNodeDirectory;

class TagStorage;
class TagSourceDirectory : public TagSource, public DirectoryListener{
public:
	TagSourceDirectory(TagStorage* storage = 0, const char* root = 0);
	~TagSourceDirectory();

	void onFileAdded(DirectoryWatch* watch, const char* path);
	void onFileRemoved(DirectoryWatch* watch, const char* path);
	void onFileModified(DirectoryWatch* watch, const char* path);
	void onFileRenamed(DirectoryWatch* watch, const char* oldName, const char* newName);

	const char* tagFilePath() const{ return tagFilePath_.c_str(); }

	// from TagSource:
	void poll(TagStorage* storage);
	void serialize(Archive& ar);
	void setStorage(TagStorage* storage);
	void reattach();
protected:
	TagSourceNode* findNodeByPath(const char* path, bool filePath = true);

	void rescan();

	bool changed_;
	DirectoryWatch* directoryWatch_;
	string tagFilePath_;
	SharedPtr<TagSourceNodeDirectory> directory_;
};

class TagSourceNodeFile;

// mainly for debug purposes
class TagSourceFileList : public TagSource
{
public:
    TagSourceFileList(TagStorage* storage);

    void add(const char* path);
    void remove(const char* path);

    void poll(TagStorage* storage);
protected:
    void removeNode(const char* path);

    typedef vector<SharedPtr<TagSourceNodeFile> > TagSourceNodeFiles;
    TagSourceNodeFiles files_;
	vector<string> newFiles_;
	vector<string> removedFiles_;
};
